package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.rcloud.io.Utils.ImageUtils;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.Utils.UploadVo;
import org.rcloud.io.model.Shoot;
import org.rcloud.io.service.ShootService;
import org.rcloud.io.service.ShootUploadService;
import org.rcloud.io.service.WxPushServiceQcl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2019/3/28.
 */

@RestController
@RequestMapping(value = "/shoot")
public class ShootController {
    @Autowired
    private ShootService shootService;

    @RequestMapping(value = "/add", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public int add(@RequestBody String params) {
        System.out.println("==========:" + params);
        Shoot shoot = JSON.parseObject(params, Shoot.class);
        Shoot shoot2 = (Shoot) shootService.selectById(shoot).getInfo();
        if (null != shoot2) {
            System.out.println("=====摄影大赛已报名！======");
            return 2;
        } else {
            System.out.println("=====摄影大赛未报名，进行报名...======");
            JSONObject object = JSONObject.parseObject(params);
            object.put("id", UUID.randomUUID());
            object.put("createtime", new Date());
            object.put("flag", 1);
            Shoot shoot1 = JSONObject.parseObject(object.toString(), Shoot.class);
            if (Integer.valueOf(shootService.insertSelective(shoot1).getInfo().toString()) == 1) {
                System.out.println("=======报名成功，推送模板消息！=======");
                WxPushServiceQcl wxPushServiceQcl = new WxPushServiceQcl();
                wxPushServiceQcl.pushOneUser3(shoot1.getUserid(), shoot1.getFramid(), shoot1.getActivityname(), shoot1.getName(),
                        shoot1.getTelephone(), shoot1.getCreatetime());
                return 1;
            } else {
                System.out.println("=======报名失败！=======");
                return 0;
            }
        }
    }

    /**
     * 查询报名统计
     */
    @RequestMapping(value = "/selectAll", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public PageVo<Shoot> selectAll() {
        return shootService.selectAll();
    }

    @Autowired
    private ShootUploadService shootUploadService;

    @RequestMapping("/upload")
    @ResponseBody
    @CrossOrigin
    public ResultVo upload(@RequestBody String params) {

        List<UploadVo> list = JSONArray.parseArray(params, UploadVo.class);

        for (UploadVo upload : list) {
            shootUploadService.upload(upload);
        }
        return ResultVo.setOK(null);

    }

    @RequestMapping("/upload1")
    @ResponseBody
    @CrossOrigin
    public ResultVo upload1(UploadVo uploadVo) {
        return shootUploadService.upload(uploadVo);

    }

    @Value("${file.uploadFolder}")
    private String uploadFolder;

    @RequestMapping("/upload2")
    @ResponseBody
    @CrossOrigin
    public Boolean t(String path) {
        //切割获取图片名称
        String[] split = path.split("/");
        String name = split[split.length - 1];

        String s = uploadFolder + "image/" + name;
        File file = new File(s);

        String s1 = uploadFolder + "images/" + name;
        File file3 = new File(s1);

        File file2 = new File("/var/lib/docker/tomcat/apache-tomcat-8.5.37/webapps/wechat-api-0.0.1-SNAPSHOT/WEB-INF/classes/static/upload/images/1559290943685.jpg");
        if (!file.exists()) {
            file.getParentFile().mkdirs();
            try {
                ImageUtils.createThumbnail(file3, file, 750, 750);
                ImageUtils.makeWatermark(file, file2, 10, 10, 0.6F);
                return true;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }
}
